
package com.jf.cloud.user.controller.multishop;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.user.dto.UserExtensionDTO;
import com.jf.cloud.user.model.UserExtension;
import com.jf.cloud.user.service.UserExtensionService;
import com.jf.cloud.user.vo.UserExtensionVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户扩展信息
 *
 * @author zz
 * @date 2020-12-08 10:16:53
 */
@RestController("multishopUserExtensionController")
@RequestMapping("/m/user_extension")
@Tag(name = "店铺-用户扩展信息")
public class UserExtensionController {

    @Autowired
    private UserExtensionService userExtensionService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取用户扩展信息列表" , description = "分页获取用户扩展信息列表")
    public ServerResponseEntity<PageVO<UserExtensionVO>> page(@Valid PageDTO pageDTO) {
        PageVO<UserExtensionVO> userExtensionPage = userExtensionService.page(pageDTO);
        return ServerResponseEntity.success(userExtensionPage);
    }

    @GetMapping
    @Operation(summary = "获取用户扩展信息" , description = "根据userExtensionId获取用户扩展信息")
    public ServerResponseEntity<UserExtensionVO> getByUserExtensionId(@RequestParam Long userExtensionId) {
        UserExtensionVO userExtensionVO = mapperFacade.map(userExtensionService.getByUserExtensionId(userExtensionId), UserExtensionVO.class);
        return ServerResponseEntity.success(userExtensionVO);
    }

    @PostMapping
    @Operation(summary = "保存用户扩展信息" , description = "保存用户扩展信息")
    public ServerResponseEntity<Void> save(@Valid @RequestBody UserExtensionDTO userExtensionDTO) {
        UserExtension userExtension = mapperFacade.map(userExtensionDTO, UserExtension.class);
        userExtension.setSignDay(0);
        userExtension.setUserExtensionId(null);
        userExtensionService.save(userExtension);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新用户扩展信息" , description = "更新用户扩展信息")
    public ServerResponseEntity<Void> update(@Valid @RequestBody UserExtensionDTO userExtensionDTO) {
        UserExtension userExtension = mapperFacade.map(userExtensionDTO, UserExtension.class);
        userExtensionService.update(userExtension);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除用户扩展信息" , description = "根据用户扩展信息id删除用户扩展信息")
    public ServerResponseEntity<Void> delete(@RequestParam Long userExtensionId) {
        userExtensionService.deleteById(userExtensionId);
        return ServerResponseEntity.success();
    }
}
